<?php
class GotoMenu extends CWidget
{
    public $items;
    public $name='goto_menu';
    public $selected='';
    public $htmlOptions=array();
    
    public function init()
    {
        parent::init();
        if ($this->items == null)
        {
            $gotoMenu = $this->controller->asa('gotoMenu');
            if (is_object($gotoMenu))
                $this->items = $gotoMenu->data;
//            Yii::trace(__CLASS__.'.'.__FUNCTION__, 'GotoMenu');
//            Yii::trace(CVarDumper::dumpAsString($this->items), 'GotoMenu');
        }
    }
    
    public function run()
    {
//        Yii::trace(__CLASS__.'.'.__FUNCTION__, 'GotoMenu');
//        Yii::trace(CVarDumper::dumpAsString($this->items), 'GotoMenu');
        if (is_array($this->items) && count($this->items))
        {
            $items = array();
            //normalize data
            foreach ($this->items as $item)
            {
                if (!isset($item['label'], $item['url']))
                    throw new CException('data Invalid');
                $items[Yii::app()->request->getHostInfo().CHtml::normalizeUrl($item['url'])] = CHtml::encode($item['label']);
            }
            $this->htmlOptions = CMap::mergeArray(array(
                'prompt'=>'Go to ...',
                'class'=>'MenuGoto',
            ), $this->htmlOptions);
            echo CHtml::dropDownList($this->name, $this->selected, $items, $this->htmlOptions);
            $id = CHtml::getIdByName($this->name);
            $script = "jQuery('#{$id}').change(function(){window.location = decodeURIComponent(jQuery(this).val())});";
            Yii::app()->clientScript->registerScript(__CLASS__.'Goto', $script);
        }
    }
}